package com.wwwarehouse.contract.orders.import_order;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.animation.Animation;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.BaseCardDeskActivity;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.SpaceItemDecoration;
import com.wwwarehouse.common.fragment.CommonBridgeWebViewFragment;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.contract.adapter.ImportOrdersMonitorRecycleAdapter;
import com.wwwarehouse.contract.bean.ImporderMonitorBean;
import com.wwwarehouse.contract.core.ContractConstant;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import contract.wwwarehouse.com.contract.R;

/**
 * 订单获取监控pager内容fragment
 * Created by ShineF on 2017/9/20 0020.
 */

public class ImportOrdersMonitorPagerContentFragment extends BaseFragment implements ImportOrdersMonitorRecycleAdapter.OnItemClickListener {

    private RecyclerView mRvContent;
    private boolean isMeasured = false;
    private ImportOrdersMonitorRecycleAdapter mAdapter;
    private ArrayList<ImporderMonitorBean.ListBean> mDataList;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_import_orders_monitor_pager_content, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mRvContent = $(R.id.rv_content);
    }


    @Override
    public void setTitle() {

    }

    @Override
    public void requestDatas() {

    }

    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        Bundle bundle = getArguments();
        if (bundle != null) {
            mDataList = bundle.getParcelableArrayList("data");
            int mPage = bundle.getInt("page");
            if (mDataList != null && mPage == 1) {
                ViewTreeObserver observer = mRvContent.getViewTreeObserver();
                observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
                    public boolean onPreDraw() {
                        if (!isMeasured) {
                            setHeight(mRvContent.getMeasuredHeight());
                        }
                        return true;
                    }
                });
                if (isMeasured && mAdapter != null) {
                    mRvContent.setAdapter(mAdapter);
                }
            } else {
                Map<String, Object> map = new HashMap<>();
                map.put("type", "1");
                map.put("itemType", "H_SHOP");
                map.put("name", "");
                Map<String, Object> queryMap = new HashMap<>();
                queryMap.put("page", mPage);
                queryMap.put("size", "9");
                map.put("query", queryMap);
                loadData(ContractConstant.GET_IMPORT_TOOLS_MONITOR_INDEX, map, 1);
            }
            mRvContent.addItemDecoration(new SpaceItemDecoration(10, 10));
            mRvContent.setLayoutManager(new GridLayoutManager(mActivity, 3));
        }
        return null;
    }

    public void loadData(String url, Map params, int what) {
        NoHttpUtils.httpPost(url, params, new NoHttpUtils.OnResponseListener() {
            @Override
            public void onSucceed(CommonClass commonClass, int i) {
                if ("0".equals(commonClass.getCode())) {
                    ImporderMonitorBean data = JSON.parseObject(commonClass.getData().toString(), ImporderMonitorBean.class);
                    setData(data);
                }
            }

            @Override
            public void onFailed(String s, int i) {

            }

            @Override
            public void onFinish(int i) {

            }
        }, what);
    }


    public void setHeight(int height) {
        isMeasured = true;
        if (mAdapter == null && isAdded()) {
            mAdapter = new ImportOrdersMonitorRecycleAdapter(mDataList, height, mActivity);
            mRvContent.setAdapter(mAdapter);
            mAdapter.setOnItemClickListener(this);
        }
    }

    @Override
    public void ItemClickListener(int position, View view, ImporderMonitorBean.ListBean data) {
        ((BaseCardDeskActivity) mActivity).hideSearchTitle();
        ((BaseCardDeskActivity) mActivity).hideSearchInput();
        CommonBridgeWebViewFragment webFragment = new CommonBridgeWebViewFragment();
        Map<String, Object> paramsMap = new HashMap<>();
        paramsMap.put("token", sp.getValue("sp_token"));
        paramsMap.put("shopId", data.getUkid());
        paramsMap.put("shopName", data.getName());
        paramsMap.put("ownerUkid", "");
        JSONObject json = new JSONObject(paramsMap);
        Bundle bundle = new Bundle();
        bundle.putString("url", "http://192.168.6.31/app/OrderMonitor/");
        bundle.putString("oppositeUrl", "http://192.168.6.31/app/OrderMonitorInstruction/");
        bundle.putString("data", json.toString());
        bundle.putString("title", "订单获取监控");
        bundle.putString("outSiteTitle", "订单下载监控说明");
        webFragment.setArguments(bundle);
        pushFragment(webFragment);
    }

    public void setData(ImporderMonitorBean data) {
        mDataList = data.getList();
        ViewTreeObserver observer = mRvContent.getViewTreeObserver();
        observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            public boolean onPreDraw() {
                if (!isMeasured) {
                    setHeight(mRvContent.getMeasuredHeight());
                }
                return true;
            }
        });
    }
}
